package com.weiyi.mashangzhu.Application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.util.NetUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.weiyi.mashangzhu.Landlord.Bean.PhotoBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.CollectInfoBean;
import com.weiyi.mashangzhu.Roomer.Bean.FlagBean;
import com.weiyi.mashangzhu.Roomer.Bean.HouseListBean;
import com.weiyi.mashangzhu.Roomer.Bean.MapInfoBean;
import com.weiyi.mashangzhu.Roomer.Bean.OrderBean;
import com.weiyi.mashangzhu.Roomer.Bean.OrderDetailsBean;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeHavePageBean;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.ThreadUtil;
import com.weiyi.mashangzhu.easemob.DemoHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by win7 on 2017/8/31.
 */

public class MyApp extends Application {


    public static String ll="";
    //房客端搜索房源—筛选
    public static String person_xuan;
    public static String price_xuan;
    public static String type_xuan;
    public static String room_xuan;
    public static String bed_type_xuan;
    public static String starttime_xuan;
    public static String endtime_xuan;
    public static String destination_xuan;
    public static String facil_xuan;
    public static List<FlagBean> flags_type_xuan = new ArrayList<>(7);
    public static List<FlagBean> flags_xuan = new ArrayList<>(8);
    public static List<String> search_type = new ArrayList<>();
    public static int search_price=-1;
    //订单详情-优惠券信息
    public static List<OrderDetailsBean.DataBean.CouponlistBean> couponlistBeanList_yes = new ArrayList<>();
    public static List<OrderDetailsBean.DataBean.CouponlistBean> couponlistBeanList_no = new ArrayList<>();


    public static String clientid;
    private List<Activity> mList = new ArrayList<Activity>();
    private List<Activity> mList_login = new ArrayList<Activity>();
    public static ArrayList<PhotoBean> images = new ArrayList<>();
    public static ArrayList<String> images_id = new ArrayList<>();
    public static List<PhotoBean> img_url = new ArrayList<>();
    public static List<PhotoBean> post_url = new ArrayList<>();
    public static List<String> post_url_finish = new ArrayList<>();
    public static List<PhotoBean> get_url = new ArrayList<>();
    public static String re_bedtype, re_bedl, re_bedw, re_bedcount;
    public static String house_title, house_content, price_price, price_yajian, price_add, price_other, base_area, base_type, base_type_ting, base_type_shi, base_type_wei, base_type_chu, base_wei,
            base_person, base_bed_type, base_bed_l, base_bed_w, base_bed_count, realname, idcard, idcard_iv1, idcard_iv2, idcard_iv3, account, phone, type, address_province, address_city, address_district, address_address, address_loudong, address_louceng, address_roomnum, facility,
            img1, img2, img3, img4, img5, img6, id1, id2, id3;
    public static  String dis_date=null;
    public static  String dis_price=null;
    public static  String dis_7=null;
    public static  String dis_30=null;

    public static List<Integer> facilitylist = new ArrayList<>();
    public static List<String> img = new ArrayList<>();
    public static List<FlagBean> flagBeanList = new ArrayList<>();
    public static List<FlagBean> flagBeanList_edit = new ArrayList<>();
    private static MyApp instance;
    private static MyApp appContext;
    public static IWXAPI mWxApi = null;
    public static   Vibrator vibrator=null;

    public static Context getAppContext() {
        return appContext;
    }
    public void openVibrato() {
        vibrator = (Vibrator) getAppContext().getSystemService(VIBRATOR_SERVICE);
        getSystemService(VIBRATOR_SERVICE);
//                    获得一个震动的服务
    }
    public void cancelVibrato() {
        vibrator.cancel();
//                    取消震动的服务
    }

    public MyApp() {
    }

    public static List getUrlList(List<ThemeHavePageBean.DataBean.HouselistBean> list, int position) {
        List<String> urllist = new ArrayList<>();
        if (list.get(position).getHImg1() != null && !list.get(position).getHImg1().equals("")) {
            urllist.add(list.get(position).getHImg1());
        }
        if (list.get(position).getHImg2() != null && !list.get(position).getHImg2().equals("")) {
            urllist.add(list.get(position).getHImg2());
        }
        if (list.get(position).getHImg3() != null && !list.get(position).getHImg3().equals("")) {
            urllist.add(list.get(position).getHImg3());
        }
        if (list.get(position).getHImg4() != null && !list.get(position).getHImg4().equals("")) {
            urllist.add(list.get(position).getHImg4());
        }
        if (list.get(position).getHImg5() != null && !list.get(position).getHImg5().equals("")) {
            urllist.add(list.get(position).getHImg5());
        }
        if (list.get(position).getHImg6() != null && !list.get(position).getHImg6().equals("")) {
            urllist.add(list.get(position).getHImg6());
        }
        return urllist;

    }

    public static List getUrlList2(List<HouseListBean> list, int position) {
        List<String> urllist = new ArrayList<>();
        if (list.get(position).getHImg1() != null && !list.get(position).getHImg1().equals("")) {
            urllist.add(list.get(position).getHImg1());
        }
        if (list.get(position).getHImg2() != null && !list.get(position).getHImg2().equals("")) {
            urllist.add(list.get(position).getHImg2());
        }
        if (list.get(position).getHImg3() != null && !list.get(position).getHImg3().equals("")) {
            urllist.add(list.get(position).getHImg3());
        }
        if (list.get(position).getHImg4() != null && !list.get(position).getHImg4().equals("")) {
            urllist.add(list.get(position).getHImg4());
        }
        if (list.get(position).getHImg5() != null && !list.get(position).getHImg5().equals("")) {
            urllist.add(list.get(position).getHImg5());
        }
        if (list.get(position).getHImg6() != null && !list.get(position).getHImg6().equals("")) {
            urllist.add(list.get(position).getHImg6());
        }
        return urllist;

    }

    public static List getUrlList3(List<MapInfoBean> list, int position) {
        List<String> urllist = new ArrayList<>();
        if (list.get(position).getHImg1() != null && !list.get(position).getHImg1().equals("")) {
            urllist.add(list.get(position).getHImg1());
        }
        if (list.get(position).getHImg2() != null && !list.get(position).getHImg2().equals("")) {
            urllist.add(list.get(position).getHImg2());
        }
        if (list.get(position).getHImg3() != null && !list.get(position).getHImg3().equals("")) {
            urllist.add(list.get(position).getHImg3());
        }
        if (list.get(position).getHImg4() != null && !list.get(position).getHImg4().equals("")) {
            urllist.add(list.get(position).getHImg4());
        }
        if (list.get(position).getHImg5() != null && !list.get(position).getHImg5().equals("")) {
            urllist.add(list.get(position).getHImg5());
        }
        if (list.get(position).getHImg6() != null && !list.get(position).getHImg6().equals("")) {
            urllist.add(list.get(position).getHImg6());
        }
        return urllist;

    }

    public static List getUrlList4(List<CollectInfoBean.DataBean> list, int position) {
        List<String> urllist = new ArrayList<>();
        if (list.get(position).getHouse().getHImg1() != null && !list.get(position).getHouse().getHImg1().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg1());
        }
        if (list.get(position).getHouse().getHImg2() != null && !list.get(position).getHouse().getHImg2().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg2());
        }
        if (list.get(position).getHouse().getHImg3() != null && !list.get(position).getHouse().getHImg3().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg3());
        }
        if (list.get(position).getHouse().getHImg4() != null && !list.get(position).getHouse().getHImg4().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg4());
        }
        if (list.get(position).getHouse().getHImg5() != null && !list.get(position).getHouse().getHImg5().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg5());
        }
        if (list.get(position).getHouse().getHImg6() != null && !list.get(position).getHouse().getHImg6().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg6());
        }
        return urllist;

    }

    public static List getUrlList5(List<OrderBean.DataBean> list, int position) {
        List<String> urllist = new ArrayList<>();
        if (list.get(position).getHouse().getHImg1() != null && !list.get(position).getHouse().getHImg1().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg1());
        }
        if (list.get(position).getHouse().getHImg2() != null && !list.get(position).getHouse().getHImg2().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg2());
        }
        if (list.get(position).getHouse().getHImg3() != null && !list.get(position).getHouse().getHImg3().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg3());
        }
        if (list.get(position).getHouse().getHImg4() != null && !list.get(position).getHouse().getHImg4().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg4());
        }
        if (list.get(position).getHouse().getHImg5() != null && !list.get(position).getHouse().getHImg5().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg5());
        }
        if (list.get(position).getHouse().getHImg6() != null && !list.get(position).getHouse().getHImg6().equals("")) {
            urllist.add(list.get(position).getHouse().getHImg6());
        }
        return urllist;

    }

    public synchronized static MyApp getInstance() {
        if (null == instance) {
            instance = new MyApp();
        }
        return instance;
    }

    public void addActivity(Activity activity) {
        mList.add(activity);
    }
    public void addActivity_login(Activity activity) {
        mList_login.add(activity);
    }
    public void removeActivity(Activity activity) {
        mList.remove(activity);
    }

    public static void changeAddress(final BaiduMap baiduMap, String city, String address) {
        // 创建地理编码检索实例
        GeoCoder mSearch = GeoCoder.newInstance();
        OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {

            public void onGetGeoCodeResult(GeoCodeResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    //没有检索到结果
                    Log.e("baiiiiiiiii", "没有检索到结果");
//                    T.showShort(context,"没有检索到结果");

                } else {
                    LatLng ll = new LatLng(result.getLocation().latitude, result.getLocation().longitude);
                    Log.e("baiiiiiiiii", "la" + result.getLocation().latitude);
                    MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                    baiduMap.animateMapStatus(u, 200);
                    //构建Marker图标
                    BitmapDescriptor bitmap = BitmapDescriptorFactory
                            .fromResource(R.mipmap.ad);
                    //构建MarkerOption，用于在地图上添加Marker
                    OverlayOptions option = new MarkerOptions()
                            .position(ll)
                            .icon(bitmap);
                    //在地图上添加Marker，并显示
                    baiduMap.addOverlay(option);
                }
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {

            }
        };
        mSearch.setOnGetGeoCodeResultListener(listener);
        mSearch.geocode(new GeoCodeOption()
                .city(city)
                .address(address));
        mSearch.destroy();


    }

    //关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
    //关闭每一个list内的activity
    public void exit_login() {
        try {
            for (Activity activity : mList_login) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    private void registToWX() {
        //AppConst.WEIXIN.APP_ID是指你应用在微信开放平台上的AppID，记得替换。
        mWxApi = WXAPIFactory.createWXAPI(this, "wx29a520b1867fe3a2", false);
        // 将该app注册到微信
        mWxApi.registerApp("wx29a520b1867fe3a2");
    }


    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;
        SDKInitializer.initialize(getApplicationContext());
        registToWX();
        DemoHelper.getInstance().init(appContext);
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
